<template>
  <div>
    <div class="ibox-content no-border no-padding-bottom">
      <el-col>
        <el-form :inline="true" :model="filterForm" size="small" label-width="80px">
          <el-form-item label="id">
            <el-input v-model="filterForm.id"></el-input>
          </el-form-item>
          <el-form-item label="标签名称">
            <el-input v-model="filterForm.tag"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click.native="filterClick" type="primary">查询</el-button>
            <el-button @click.native="resetFilterForm('filterForm')">重置</el-button>
          </el-form-item>
          <div class="hr-line-dashed"></div>
          <br>
          <el-form-item size="small">
            <el-button @click="handleAdd" type="primary" plain icon="el-icon-plus">添加标签</el-button>
            <el-button @click="handleDelMore" type="danger" plain icon="el-icon-delete">删除标签</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </div>
    <div class="ibox-content">
      <!--表格-->
      <el-table
        ref="multipleTable"
        :data="arrList"
        size="medium"
        border
        highlight-current-row
        tooltip-effect="dark"
        style="width: 100%"
        :max-height="500"
        v-loading="tableLoading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="ID" width="80"></el-table-column>
        <el-table-column prop="tag" label="标签名称"></el-table-column>
        <el-table-column prop="bg_color" label="背景颜色" width="150"></el-table-column>
        <el-table-column label="操作" align="center" width="130" fixed="right">
          <template slot-scope="scope">
            <el-tooltip @click.native="handleEdit(scope.row)" :enterable="false" effect="dark" content="编辑"
                        placement="top-start">
              <el-button type="primary" plain size="mini" icon="el-icon-edit"></el-button>
            </el-tooltip>
            <el-tooltip @click.native="handleDelete(scope.row)" :enterable="false" effect="dark" content="删除"
                        placement="top-start">
              <el-button type="danger" plain size="mini" icon="el-icon-delete"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!--分页工具条-->
      <div v-show="arrList.length>0" style="margin-top: 18px;text-align: center">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pageInfo.currPage"
          :page-sizes="[20]"
          :page-size="pageInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.totalCount">
        </el-pagination>
      </div>
    </div>


    <!-- 添加标签 -->
    <el-dialog width="24%" :title="tagTitle" :visible.sync="addTagFormVisible" @close="closeDialog"
               :close-on-click-modal="false">
      <el-form :rules="addTagFormRule" :model="addTagForm" ref="addTagForm" size="small">
        <el-form-item prop="tag" label="标签名称">
          <el-input v-model="addTagForm.tag" placeholder="请输入标签名称"></el-input>
        </el-form-item>
        <el-form-item label="背景色">
          <el-input v-model="addTagForm.bg_color" placeholder="请输入背景色的16进制编码"></el-input>
        </el-form-item>
        <el-form-item label="文字颜色">
          <el-input v-model="addTagForm.font_color" placeholder="请输入文字颜色的16进制编码"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="closeDialog" size="small">关闭</el-button>
        <el-button type="primary" @click.native="addTagFormSave('addTagForm')" size="small">保存</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
  import {tagsList, saveTag, deleteTag} from "../../api/product";

  export default {
    name: "Label",
    data() {
      return {
        filterForm: {},
        arrList: [],
        pageInfo: {
          totalPage: 0,// 总页数
          totalCount: 0,// 总条数
          pageSize: 0,// 每页条数
          currPage: 1// 当前页
        },
        tableLoading: false,
        tableSelectData: [],
        addTagForm: {},
        addTagFormRule: {
          tag: [
            {required: true, message: '请输入标签名称'}
          ],
        },
        addTagFormVisible: false,
        tagTitle: '',
      }
    },
    created() {
      this.getTagList();
    },
    methods: {
      //查询
      filterClick() {
        this.pageInfo.currPage = 1;
        this.getTagList();
      },
      //重置
      resetFilterForm(form) {
        this.$refs[form].resetFields();
        this.filterForm = {};
        this.pageInfo.currPage = 1;
        this.getTagList();
      },
      //分页
      handleCurrentChange(val) {
        // console.log(val);
        this.pageInfo.currPage = val;
        this.getTagList();
      },
      handleSelectionChange(data) {
        this.tableSelectData = data;
      },
      //单个删除
      handleDelete(row) {
        this.$confirm('删除后，数据将不可恢复!', `确定要删除标签"${row.tag}"吗`, {
          confirmButtonText: '删除',
          cancelButtonText: '取消',
          type: 'warning',
          center: true
        }).then(() => {
          this.commonDelMethods(row.id)
        }).catch(() => {

        })
      },
      commonDelMethods(id) {
        deleteTag({id: id}).then(res => {
          this.$message.success(res.msg);
          this.getTagList();
        }).catch(error => {

        })
      },
      //批量上架
      handleDelMore() {
        if (this.tableSelectData.length > 0) {
          let ids = [];
          this.tableSelectData.forEach((item) => {
            ids.push(item.id)
          });
          if (ids.length > 0) {
            this.$confirm('删除后，数据将不可恢复!', '确定要删除选择的标签吗?', {
              confirmButtonText: '删除',
              cancelButtonText: '取消',
              type: 'warning',
              center: true
            }).then(() => {
              this.commonDelMethods(ids.toString())
            }).catch(() => {

            })
          }
        } else {
          this.$message.warning('请选择要删除的标签')
        }
      },
      getTagList() {
        this.tableLoading = true;
        tagsList({
          page: this.pageInfo.currPage,
          ...this.filterForm
        }).then(res => {
          this.tableLoading = false;
          const {listData, page} = res.data;
          this.arrList = listData;
          this.pageInfo = page
        }).catch(() => {
          this.tableLoading = false
        })
      },
      //添加标签
      handleAdd() {
        this.tagTitle = '添加标签';
        this.addTagFormVisible = true;
        this.addTagForm = {}
      },
      //编辑
      handleEdit(row) {
        this.tagTitle = '编辑标签';
        this.addTagFormVisible = true;
        this.addTagForm = Object.assign({}, row);
      },

      closeDialog() {
        this.addTagFormVisible = false;
        this.$refs['addTagForm'].clearValidate();
      },
      addTagFormSave(form) {
        this.$refs[form].validate(valid => {
          if (valid) {
            saveTag({
              ...this.addTagForm
            }).then(res => {
              this.addTagFormVisible = false;
              this.$message.success(res.msg);
              this.getTagList();
            }).catch(error => {

            })
          }
        })
      },
    }
  }
</script>

<style scoped>

</style>
